﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ReportBusiness;
public partial class Login : System.Web.UI.Page
{
    private uMESLoginBusiness bll = new uMESLoginBusiness();
    ADHelper adh = new ADHelper();
    string IP = System.Configuration.ConfigurationManager.AppSettings["IP"];
    private string giLoginUserName = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        passwordTxtBox.Attributes.Add("onkeydown", "submitKeyClick('loginButton');");
        giLoginUserName = Request.QueryString["giLoginUserName"] == null ? "" : Request.QueryString["giLoginUserName"].ToString();
        if (!IsPostBack)
        {
            AutoLonin(giLoginUserName);
        }
    }

    //系统超链接登录
    protected void AutoLonin(string username)
    {
        if (username != "")
        {
            DataTable dtEmployeeInfo = bll.getEmployeeInfo(username);
            txtEmployeeid.Text = dtEmployeeInfo.Rows[0]["employeeid"].ToString();//用户id
            DataTable dtPages = bll.getPageListByEmployeeId(txtEmployeeid.Text);// 获取当前用户可访问的页面集合

            // add by zhunan 2017-7-7 用自定义dto类传值
            EmployeeInfoDTO dto = new EmployeeInfoDTO();
            dto.Employeeid = txtEmployeeid.Text;
            dto.EmployeeName = dtEmployeeInfo.Rows[0]["fullname"].ToString(); ;
            dto.FactoryName = dtEmployeeInfo.Rows[0]["factoryname"].ToString();
            dto.Workcenter = dtEmployeeInfo.Rows[0]["workcentername"].ToString();
            dto.FactoryId = dtEmployeeInfo.Rows[0]["factoryid"].ToString();
            dto.WorkcenterId = dtEmployeeInfo.Rows[0]["workcenterid"].ToString();

            //Modify by Yuxj20190424增加班查询
            if (string.IsNullOrEmpty(dtEmployeeInfo.Rows[0]["shiftname"].ToString()))
            {
                dto.ShiftName = "";
            }
            else
            {
                dto.ShiftName = dtEmployeeInfo.Rows[0]["shiftname"].ToString();
            }

            if (string.IsNullOrEmpty(dtEmployeeInfo.Rows[0]["shiftid"].ToString()))
            {
                dto.ShiftId = "";
            }
            else
            {
                dto.ShiftId = dtEmployeeInfo.Rows[0]["shiftid"].ToString();
            }


            if (string.IsNullOrEmpty(dtEmployeeInfo.Rows[0]["teamname"].ToString()))
            {
                dto.TeamName = "";
            }
            else
            {
                dto.TeamName = dtEmployeeInfo.Rows[0]["teamname"].ToString();
            }
            if (string.IsNullOrEmpty(dtEmployeeInfo.Rows[0]["teamid"].ToString()))
            {
                dto.TeamId = "";
            }
            else
            {
                dto.TeamId = dtEmployeeInfo.Rows[0]["teamid"].ToString();
            }

            dto.DtPages = dtPages;

            Session["employeeinfo"] = dto;

            HttpCookie userid = new HttpCookie("userid", dtEmployeeInfo.Rows[0]["employeeid"].ToString());
            HttpCookie factoryid = new HttpCookie("factoryid", dtEmployeeInfo.Rows[0]["factoryid"].ToString());
            HttpCookie workcenterid = new HttpCookie("workcenterid", dtEmployeeInfo.Rows[0]["workcenterid"].ToString());
            HttpCookie shiftid = new HttpCookie("shiftid", dtEmployeeInfo.Rows[0]["workcenterid"].ToString());
            HttpCookie teamid = new HttpCookie("teamid", dtEmployeeInfo.Rows[0]["teamid"].ToString());

            Response.AppendCookie(userid);
            Response.AppendCookie(factoryid);
            Response.AppendCookie(workcenterid);
            Response.AppendCookie(shiftid);
            Response.AppendCookie(teamid);
            Response.Redirect("direct.html?uid=" + dto.Employeeid + "&host=" + IP);
        }
    }

    //点击"登录"按钮方式登录
    protected void loginButton_Click(object sender, EventArgs e)
    {
        string strUid = this.userNameTxtBox.Text;
        string strEncryPwd = adh.Encrypto(this.passwordTxtBox.Text);// 解密后的密码
        string password = this.passwordTxtBox.Text;                 // 用户输入的原始密码

        string info = "";
        bool bol = bll.Login(strUid, password, out info);

        if (bol)
        {
            DataTable dtEmployeeInfo = bll.getEmployeeInfo(strUid);
            txtEmployeeid.Text = dtEmployeeInfo.Rows[0]["employeeid"].ToString();//用户id
            DataTable dtPages = bll.getPageListByEmployeeId(txtEmployeeid.Text);// 获取当前用户可访问的页面集合

            EmployeeInfoDTO dto = new EmployeeInfoDTO();
            dto.Employeeid = txtEmployeeid.Text;
            dto.EmployeeName = dtEmployeeInfo.Rows[0]["fullname"].ToString();
            dto.FactoryName = dtEmployeeInfo.Rows[0]["factoryname"].ToString();
            dto.Workcenter = dtEmployeeInfo.Rows[0]["workcentername"].ToString();
            dto.FactoryId = dtEmployeeInfo.Rows[0]["factoryid"].ToString();
            dto.WorkcenterId = dtEmployeeInfo.Rows[0]["workcenterid"].ToString();

            if (string.IsNullOrEmpty(dtEmployeeInfo.Rows[0]["teamname"].ToString()))
            {
                dto.TeamName = "";
            }
            else
            {
                dto.TeamName = dtEmployeeInfo.Rows[0]["teamname"].ToString();
            }

            if (string.IsNullOrEmpty(dtEmployeeInfo.Rows[0]["teamid"].ToString()))
            {
                dto.TeamId = "";
            }
            else
            {
                dto.TeamId = dtEmployeeInfo.Rows[0]["teamid"].ToString();
            }

            //Modify by Yuxj20190424增加班查询
            if (string.IsNullOrEmpty(dtEmployeeInfo.Rows[0]["shiftname"].ToString()))
            {
                dto.ShiftName = "";
            }
            else
            {
                dto.ShiftName = dtEmployeeInfo.Rows[0]["shiftname"].ToString();
            }

            if (string.IsNullOrEmpty(dtEmployeeInfo.Rows[0]["shiftid"].ToString()))
            {
                dto.ShiftId = "";
            }
            else
            {
                dto.ShiftId = dtEmployeeInfo.Rows[0]["shiftid"].ToString();
            }

            dto.DtPages = dtPages;
            Session["employeeinfo"] = dto;

            HttpCookie userid = new HttpCookie("userid", dtEmployeeInfo.Rows[0]["employeeid"].ToString());
            HttpCookie factoryid = new HttpCookie("factoryid", dtEmployeeInfo.Rows[0]["factoryid"].ToString());
            HttpCookie workcenterid = new HttpCookie("workcenterid", dtEmployeeInfo.Rows[0]["workcenterid"].ToString());
            HttpCookie shiftid = new HttpCookie("shiftid", dtEmployeeInfo.Rows[0]["workcenterid"].ToString());
            HttpCookie teamid = new HttpCookie("teamid", dtEmployeeInfo.Rows[0]["teamid"].ToString());

            Response.AppendCookie(userid);
            Response.AppendCookie(factoryid);
            Response.AppendCookie(workcenterid);
            Response.AppendCookie(shiftid);
            Response.AppendCookie(teamid);
            Response.Redirect("direct.html?uid=" + dto.Employeeid + "&host=" + IP);
        }
        else
        {
            Response.Write("<script type='text/javascript'>alert('" + info + "');</script>");
        }
    }
}
